package com.liehacker.android.mapservice;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

import com.liehacker.android.R;

public class C24_ReadLocation extends Activity{
	
	
	TextView		tvStatus;
	TextView		tvLocation;
	LocationManager	mLocMan;
	String			mStrProvider;
	int				mCount;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c24_readlocation);
		
		tvStatus	=	(TextView)findViewById(R.id.tvStatus);
		tvLocation 	= 	(TextView)findViewById(R.id.tvC24Location);
		
		mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);		
		mStrProvider = mLocMan.getBestProvider(new Criteria(), true);
			
	}
	
	public void onResume() {
		super.onResume();
		
		mCount = 0;
		mLocMan.requestLocationUpdates(mStrProvider, 3000, 10, mListener);
		tvStatus.setText("현재 상태 : 서비스 시작");
	}
	
	public void onPuase(){
		super.onPause();
		
		mLocMan.removeUpdates(mListener);
		tvStatus.setText("현재 상태 : 서비스 정지");
	}
	
	LocationListener	mListener = new LocationListener() {

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			mCount++;
			String strLoc = String.format("수신회수:%d\n위도:%f\n경도:%f\n고도:%f", mCount,
												location.getLatitude(), location.getLongitude(),
												location.getAltitude());
			tvLocation.setText(strLoc);
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			tvStatus.setText("현재 상태 : 서비스 사용 불가");
		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			tvStatus.setText("현재 상태 : 서비스 사용가능");
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			String strStatus = "";
			switch(status) {
			case LocationProvider.OUT_OF_SERVICE :
				strStatus = "범위 벗어남";
				break;
			case LocationProvider.AVAILABLE :
				strStatus = "사용 가능";
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE :
				strStatus = "일시적 불능";
				break;
			}
			
			tvStatus.setText(provider + "상태 변경 : " + strStatus );
		}
		
	};
}
